package com.example.shopping.user.controller;


import com.example.shopping.JsonResult;
import com.example.shopping.user.entity.User;
import com.example.shopping.user.entity.Userfenye;
import com.example.shopping.user.mapper.UserMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/api/user")
public class UserController {

        @Autowired
        UserMapper userMapper;

    //分页查询用户  必须要得参数currPage   pageSize
    @GetMapping("/list")
    public JsonResult getFyGoodsList(int currPage,int pageSize) {
        currPage=(currPage-1)*pageSize;
        List<User> user = userMapper.getUserList(currPage,pageSize);
        int count=userMapper.getusercount();
        Userfenye fen=new Userfenye();
        fen.setTotal(count);
        fen.setCurrPage(currPage);
        fen.setPageSize(pageSize);
        fen.setUser(user);
        return new JsonResult(true,"查询成功",fen);
    }

//    根据id查询用户信息  必须要的参数   用户id
    @GetMapping("/userid")
    public JsonResult getidcx(int uid){
//        System.out.println(uid);
        List<User> users =userMapper.getUseridList(uid);
        return new JsonResult(true,"用户id查询成功",users);
    }

    //用户添加 用户名称，用户账号，用户密码，邮箱，手机号，角色，状态 ;
    @PostMapping("/useradd")
    public JsonResult getuserinsert(String name,String account,String password,String email,String phone,String role){
        System.out.println(name);
        System.out.println(account);
        System.out.println(password);
        System.out.println(email);
        System.out.println(phone);
        System.out.println(role);
        int index=userMapper.getuser(name,account,password,email,phone,role);
        return new JsonResult(true,"添加成功",index);
    }

    //修改用户状态
    @GetMapping("/type")
    public JsonResult getidcxrve(int staute,int id){
        int i=userMapper.getuserid(staute, id);
        JsonResult jsonResult=new JsonResult(true,"设置状态成功",i);
        if (i==1){
            return jsonResult;
        }else {
            return new JsonResult(false, "设置状态失败", null);
        }
    }

//    删除单个用户
        @GetMapping("/del")
        public JsonResult getdel(int id){
            int ok=userMapper.getdel(id);
            JsonResult jsonResult=new JsonResult(true,"删除成功",ok);
            if (ok==1){
                return jsonResult;
            }else {
                return new JsonResult(false, "删除失败", null);
            }
        }
            //设置角色
            @GetMapping("/updaterole")
            public JsonResult getupdate(String role, int id){
                System.out.println(role);
                System.out.println(id);
                int ok=userMapper.getupdaterole(role,id);
                JsonResult jsonResult=new JsonResult(true,"设置角色成功",ok);
                if (ok==1){
                    return jsonResult;
                }else {
                    return new JsonResult(false, "设置角色失败", null);
                }
            }

//         编辑用户
            @GetMapping("/userupdate")
            public JsonResult getupdate(String name,String account,String password,String email,String phone,int id){
                System.out.println(name);
                System.out.println(account);
                System.out.println(password);
                System.out.println(email);
                System.out.println(phone);
                System.out.println(id);
                int ok=userMapper.getuserupdate(name,account,password,email,phone,id);
                JsonResult jsonResult=new JsonResult(true,"更新成功",ok);
                if (ok==1){
                    return jsonResult;
                }else {
                    return new JsonResult(false, "更新失败", null);
                }
            }

            @GetMapping("/mohu")
            public JsonResult getirerdcx(String name){
                System.out.println(name);
//                String b="%"+name+"%";
                List<User> users =userMapper.usermohu(name);
                return new JsonResult(true,"模糊查询成功！",users);
            }
}
